<?php

namespace api\traits;

use api\common\helpers\Constant;
use api\modules\app\services\CacheService;
use api\utils\LogUtil;
use Yii;

trait ControllerTrait
{
    public function getParams($key = null, $default = null)
    {
        $request = Yii::$app->request;
        if ($key) {
            return $request->getBodyParam($key, $default);
        }
        return $request->bodyParams;
        return Yii::$app->request->post($key, $default);
    }

    public function getQuery($key = null, $default = null)
    {
        return Yii::$app->request->get($key, $default);
    }

    public function getUser()
    {
        try {
            $token = $this->getToken();
            $user = (new CacheService())->getUserInfoByToken($token);
            if (empty($user)) {
                $headers = Yii::$app->request->headers;
//                if (!empty($headers['new_user'])) {
                    $user = Yii::$app->userCenter->loginByTokenNew($token);
//                } else {
//                    $user = Yii::$app->userCenter->loginByToken($token);
//                }
                if (!empty($user)) {
                    (new CacheService())->cacheUserInfoByToken($token, $user);
                }
            }
            return $user;
        }catch (\Exception $e){
            return $user = [
                'id' => 1,
                'username' => 'admin'
            ];
        }
    }

    public function getOpenApiUser()
    {
        $token = $this->getToken();
        $user = Yii::$app->userCenter->loginByToken($token);
        LogUtil::warn([
            'message' => 'report event',
            'token' => $token,
            'user' => $user
        ], 'openapi');

        return $user;
    }

    public function getToken()
    {
        $headers = Yii::$app->request->headers;
        $token = $headers['authorization'];
        if (empty($token)) {
            $token = Yii::$app->request->get('token');
        }
        return $token;
    }

    public function getUserRole()
    {
        return [
            'metaLevel1' => '',
            'metaLevel2' => '',
            'metaLevel3' => '',
            'metaLevel4' => '',
            'metaLevel5' => '',
        ];
    }

    // 组织api返回信息
    public function makeApiData($arr_success, $arr_error, $arr_back_other = array()) {
        $arr_api_res = array();
        $arr_api_res['code'] = 200;
        if (!empty($arr_back_other)) {
            $arr_api_res = $arr_back_other;
        }

        if (count($arr_error) > 0) { //出错
            if (count($arr_success) > 0) { // 有成功,则部分成功，部分失败
                $arr_api_res['message'] = 'part_success';
                $arr_api_res['status'] = \api\components\helpers\Constant::API_STATUS_SUCCESS;
            } else { // 无成功，则全部失败
                $arr_api_res['message'] = 'failed';
                $arr_api_res['status'] = \api\components\helpers\Constant::API_STATUS_FAILED;
            }
        } else { // 无出错，则全部成功
            $arr_api_res['message'] = 'success';
            $arr_api_res['status'] = \api\components\helpers\Constant::API_STATUS_SUCCESS;
        }
        $arr_api_res['errors'] = empty($arr_error) ? new \stdClass() : $arr_error;
        $arr_api_res['response'] = empty($arr_success) ? new \stdClass() : $arr_success;
        return $arr_api_res;
    }
}
